export default {
  namespaced: true,

  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '',
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 重定向
    redirectInfo: null
  }),

  mutations: {
    // 更新重定向信息
    updateRedirectInfo(state, info) {
      state.redirectInfo = info
      console.log(state.redirectInfo)
    },
    // 更新用户信息
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo
      this.commit('m_user/saveUserInfoToStorage')
    },
    // 将用户信息持久化存储到本地
    saveUserInfoToStorage(state){
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    
    // 更新token字符串
    updateToken(state, token){
      state.token = token
      this.commit('m_user/saveTokenToStorage')
    },
    // 存储token
    saveTokenToStorage(state){
      uni.setStorageSync('token', state.token)
    },
    
    updateAddress(state, address) {
      state.address = address

      this.commit('m_user/saveAddressToStorage')
    },

    saveAddressToStorage(state) {
      uni.getStorageSync('address', JSON.stringify(state.address))
    }
  },

  getters: {

   // 收货详细地址的计算属性
     addstr(state) {
       if (!state.address.provinceName) return ''
   
       // 拼接 省，市，区，详细地址 的字符串并返回给用户
       return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
     }
  },
  
}
